const app = getApp();
import {
  userRole,
  decrypt,
  addWxUser
} from "../../../server/api";
Page({
  data: {
    oss_url: app.globalData.oss_url,
    list: [],
    user_info: {},
    navigation_show: true,
    active:2,
    userRole:{},
    roleStatus:false,
    popupShow:false
  },
  onLoad: function () {
   // this.getUserRole()
    this.get_user_info()
  },
  onShow: function () {
    if(typeof this.getTabBar=== 'function' && this.getTabBar()){
      this.getTabBar().setData({
        active:2
      })
    }
    this.setData({
      user_info: app.globalData.user_info,
      haveORno:app.globalData.holdGifts,
    })
    console.log(app.globalData.user_info)
    let that=this;
    wx.getStorage({
      key: 'user_info',
      success (res) {
        console.log(res.data)
        that.setData({
          user_info:res.data
        })
        wx.getStorage({
          key: 'user_role',
          success(res){
            that.setData({
              userRole:res.data,
              roleStatus:true
            })
          }
        })
      }
    })
  },
  getUserRole(){
    let that=this;
    let data={
    //  phone:app.globalData.user_info.phone
      phone:15500000002
    }
    userRole(data).then(res=>{
      if(res.code==0){
        console.log(res)
        this.get_user_info()
        if(res.data!=null){
          wx.setStorage({
            data: res.data[0],
            key: 'user_role',
          })
          app.globalData.user_info.userDeptId=res.data[0].userDeptId
          that.setData({
            userRole:res.data[0],
            roleStatus:true,
            popupShow:false
          })
        }else{
          wx.showToast({
            title: res.errMsg,
            icon:'none'
          })
        }
      }
    })
  },
  goPage(e){
     let index=e.currentTarget.dataset.tag;
     switch(index){
       case "1":     //我的订单
        wx.navigateTo({
          url: '/pages/goodsOrder/orderList/index'
        })
        case "2":     //礼金包
        wx.switchTab({
          url: '/pages/gift/myGiftBag/index'
        })
        break;
        case "3":     //销售经理 扫码页
        wx.navigateTo({
          url: '/pages/mine/saoCode/index'
        })
        break;
     }
  },
  to_show(e) {
    wx.navigateTo({
      url: '/pages/gift/show/index?id=' + e.currentTarget.dataset['id']
    })
  },
  get_phone(e) {
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let obj = {}
      obj['openId'] = app.globalData.user_info.openId
      obj['session_key'] = app.globalData.session_key
      obj['encryptedData'] = e.detail.encryptedData
      obj['iv'] = e.detail.iv
      decrypt(obj).then(res => {
        console.log(res)
        if (res.code == 0) {
          app.globalData.user_info.phone = res.data.phone
          this.setData({ phone: app.globalData.user_info.phone })
          this.getUserRole()
        }
      })
    }else{
      wx.showToast({
        title: '您取消了手机号授权',
        icon:'none'
      })
    } 
  },
  get_user_info() {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        app.globalData.user_info['nickName'] = res.userInfo.nickName
        app.globalData.user_info['avatarUrl'] = res.userInfo.avatarUrl
        this.setData({
          user_info: res.userInfo,
          popupShow:true
        })
        wx.setStorage({
          data: res.userInfo,
          key: 'user_info',
        })
      }
    })

  },
  cancelAuthor(e){
    this.setData({
      popupShow:false
    })
  },
  to_activity(){
    wx.navigateTo({
      url: '/pages/mine/activity/index',
      // url: '/pages/main/sharePage/index?scene=1,150,1288,ACT000058'
    })
  },
  coupon_code(){
    wx.navigateTo({
      url: '/pages/mine/myCouponCode/index',
    })
  },
  to_gift(){
    wx.navigateTo({
      url: '/pages/mine/gift/index',
    })
  },
  onPageScroll: function (e) {
    if (e.scrollTop >= 30) {
      this.setData({
        navigation_show: false
      })
    } else {
      this.setData({
        navigation_show: true
      })
    }
  }
})